// 引入pinia
import { defineStore } from "pinia";

import { reqGoodsDetail, reqAddToCart } from "@/api/detail";
import type { SkuInfoStoreModel } from "@/api/model/detailModel";

export const useGoodsDetail = defineStore("GoodsDetail", {
  state: (): SkuInfoStoreModel => {
    return {
      skuInfo: {
        id: 0,
        skuName: "",
        price: 0,
        skuDesc: "",
        weight: "",
        skuDefaultImg: "",
        skuImageList: [],
      },
      skuNum: "1",
    };
  },
  actions: {
    // 获取商品详情
    async getGoodsDetail(skuId) {
      // console.log(skuId);
      const res = await reqGoodsDetail(skuId);
      // console.log("goods", res);
      this.skuInfo = res.skuInfo;
    },
    // 添加购物车
    async addToCart() {
      await reqAddToCart(this.skuInfo.id, this.skuNum);
    },
  },
});
